import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  { path: "/", redirect: "/login" },
  {
    path: '/login',
    name: 'login',
    component: () => import( '../views/login.vue')
  },
  {
    path: '/index',
    name: 'index',
    component: () => import( '../views/index.vue'),
    children:[
      { 
          path: '/role',
          name: 'role',
          meta:{
            title:'角色管理'
          },
          component: () => import( '../views/role.vue')
      },
      { 
        path: '/user',
        name: 'user',
        meta:{
          title:'用户管理'
        },
        component: () => import( '../views/user.vue')
    },
    { 
      path: '/menu',
      name: 'menu',
      meta:{
        title:'菜单管理'
      },
      component: () => import( '../views/menu.vue')
  },
  { 
    path: '/category',
    name: 'category',
    meta:{
      title:'商品分类'
    },
    component: () => import( '../views/category.vue')
},
{
  path: '/specs',
  name: 'specs',
  meta:{
    title:'商品规格'
  },
  component: () => import( '../views/specs.vue')
}
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next)=>{
 
  if(localStorage.getItem('token')){
    next()
  }
  else{
    if(to.path == "/login"){
      next()
    }
    else {
      next('/login')
    }
  }
})

export default router
